﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleKit.other
{
    public class XMLComment
    {

        XmlDocument doc;
        Dictionary<XmlNode, List<XmlNode>> dictNode = new Dictionary<XmlNode, List<XmlNode>>();
        public XMLComment()
        {

        }

        /// <summary>
        /// Remove All Xml Comment
        /// </summary>
        public void RemoveXmlComment()
        {
            string configFile = "other\\1.xml";
            doc = new XmlDocument();
            doc.Load(configFile);

            XmlNodeList nodeList = doc.SelectNodes("//configuration");

            XmlNode root = doc.DocumentElement;

            RemoveXml(root);

            foreach (XmlNode node in dictNode.Keys)
            {
                foreach(XmlNode n in dictNode[node])
                    node.RemoveChild(n);
            }

                //if (node.NodeType == XmlNodeType.Comment)
                //{
                //    int i = 0;
                //}
            doc.Save("other\\2.xml");
        }

        private void RemoveXml(XmlNode parent)
        {
            List<XmlNode> list = new List<XmlNode>();
            foreach (XmlNode node in parent.ChildNodes)
            {
                if (node.HasChildNodes == true && node.ChildNodes.Count != 0)
                {
                    RemoveXml(node);
                }
                else if (node.HasChildNodes == false)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                        list.Add(node);
                }
                else
                {
                    int i = 0;
                }

            }
            dictNode.Add(parent, list);
        }

    }
}
